package com.hexacta.hrs.web.struts.actions.feedbacks;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.vectrics.employee.Employee;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;

import com.hexacta.hrs.domain.feedback.Period;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.feedbacks.PeriodService;

public class FinalResultsRowData {

    private String newPerformance;

    private String newCommitment;

    private JobFunction newFinalBand;

    private Long newFinalPosition;

    private Employee employee;

    private Period period;

    private EmployeeService employeeService;

    private PeriodService periodService;

    private Row data;

    public FinalResultsRowData(final EmployeeService employeeService, final PeriodService periodService, final Row data) {

        this.employeeService = employeeService;
        this.periodService = periodService;

        this.data = data;

        setEmployee();
        setFinalBand();
        setFinalPosition();
        setPeriod();
        setNewCommitment();
        setNewPerformance();

    }

    /**
     * 
     */
    private void setPeriod() {
        Date start = data.getCell(5).getDateCellValue();
        Calendar cal = new GregorianCalendar();
        cal.setTime(start);

        Integer year = cal.get(Calendar.YEAR);
        period = periodService.getPeriodByYear(year.toString());
    }

    /**
     * 
     */
    private void setFinalPosition() {
        double pos = data.getCell(16).getNumericCellValue();
        newFinalPosition = Long.valueOf((long) pos);
    }

    /**
     * 
     */
    private void setFinalBand() {
        String function = data.getCell(15).getStringCellValue();
        List<JobFunction> functions = RecruitServiceLocator.getJobProvider().findAllJobFunctions();
        for (JobFunction f : functions) {
            if (f.getDescription().equals(function)) {
                newFinalBand = f;
                break;
            }
        }
    }

    /**
     * 
     */
    private void setEmployee() {
        double id = data.getCell(0).getNumericCellValue();
        employee = employeeService.searchById((long) id);

    }

    /**
     * @param newCommitment
     *            the newCommitment to set
     */
    private void setNewCommitment() {
        String comm = data.getCell(11).getStringCellValue();
        newCommitment = comm;
    }

    /**
     * @param newPerformance
     *            the newEffort to set
     */
    private void setNewPerformance() {
        String effort = data.getCell(10).getStringCellValue();
        newPerformance = effort;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @return the newFinalBand
     */
    public JobFunction getNewFinalBand() {
        return newFinalBand;
    }

    /**
     * @return the newFinalPosition
     */
    public Long getNewFinalPosition() {
        return newFinalPosition;
    }

    /**
     * @return the period
     */
    public Period getPeriod() {
        return period;
    }

    /**
     * @return the newEffort
     */
    public String getNewPerformance() {
        return newPerformance;
    }

    /**
     * @return the newCommitment
     */
    public String getNewCommitment() {
        return newCommitment;
    }

}
